<?php

class mypaging{
	static public function paging($totalPage, $currentPage, $minPage, $previousPage, $nextPage, $maxPage)
	{
	    $minPage = ($currentPage - 1 > 1 ? ($currentPage - 1) : 1);
	    $maxPage = ($currentPage + 1 < $totalPage ? ($currentPage + 1) : $totalPage);
	    $previousPage = $currentPage - 1;
	    $nextPage = $currentPage + 1;
	}
	

	static public function showPage($totalPage, $page, $url)
	{
	    if ($totalPage > 1)
	    {
	        $minPage = 0;
	        $maxPage = 0;
	        $previousPage = 0;
	        $nextPage = 0;
	        #mypaging::paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);
	
	        $urlToFirst = sprintf($url, 1);
	        $urlToPrevious = sprintf($url, $previousPage);
	        $urlToNext = sprintf($url, $nextPage);
	        $urlToLast = sprintf($url, $totalPage);
	
	        if ($minPage == 1)
	        {
	            $linkToFirst = '';
	        }
	        else
	        if ($minPage == 2)
	        {
	            $linkToFirst = link_to(1, $urlToFirst);
	        }
	        else
	        {
	            $linkToFirst = link_to(1, $urlToFirst).'...';
	        }
	
	        //$linkToPrevious = ($previousPage < 1 ? '' : link_to(PAGE_PREVIOUS, $urlToPrevious));
	
	        if ($maxPage == $totalPage)
	        {
	            $linkToLast = '';
	        }
	        else
	        if ($maxPage == $totalPage - 1)
	        {
	            $linkToLast = link_to($totalPage, $urlToLast);
	        }
	        else
	        {
	            $linkToLast = '<li>...</li><li>'.link_to($totalPage, $urlToLast).'</li>';
	        }
	
	        //$linkToNext = ($nextPage > $totalPage ? '' : link_to(PAGE_NEXT, $urlToNext));
	        $linkToPage = '';
	        for ($i = $minPage; $i <= $maxPage; $i++)
	        {
	            $urlToPage = sprintf($url, $i);
	            $linkToPage.=($i == $page ?'<li><a class="page_current">'.$i.'</a></li>' :'<li>'.link_to($i, $urlToPage).'</li>'.'');
	        }
	        echo  '<div>Trang: '.
	        '
	                     <ul>   
	                         <li>'.$linkToFirst.'</li>'.                         
	        $linkToPage.
	        '<li>'.$linkToLast.'</li>
	                     </ul>
	                   </div>
	               ';        
	    }
	}
	
	static public  function showPage_1($totalPage, $page, $url)
	{
	    if ($totalPage > 1)
	    {
	        $minPage = 0;
	        $maxPage = 0;
	        $previousPage = 0;
	        $nextPage = 0;
	        #mypaging::paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);
	
	        $urlToFirst = sprintf($url, 1);
	        $urlToPrevious = sprintf($url, $previousPage);
	        $urlToNext = sprintf($url, $nextPage);
	        $urlToLast = sprintf($url, $totalPage);
	
	        if ($minPage == 1)
	        {
	            $linkToFirst = '';
	        }
	        else
	        if ($minPage == 2)
	        {
	            $linkToFirst = '<a href="'.$urlToFirst.'">1</a>';//link_to(1, $urlToFirst);
	        }
	        else
	        {
	            $linkToFirst = '<a href="'.$urlToFirst.'">1</a>...';//link_to(1, $urlToFirst).'...';
	        }
	
	        //$linkToPrevious = ($previousPage < 1 ? '' : link_to(PAGE_PREVIOUS, $urlToPrevious));
	
	        if ($maxPage == $totalPage)
	        {
	            $linkToLast = '';
	        }
	        else
	        if ($maxPage == $totalPage - 1)
	        {
	            $linkToLast = '<a href="'.$urlToLast.'">'.$totalPage.'</a>';//link_to($totalPage, $urlToLast);
	        }
	        else
	        {
	            $linkToLast = '<li>...</li><li><a href="'.$urlToLast.'">'.$totalPage.'</a>';//link_to($totalPage, $urlToLast).'</li>';
	        }
	
	        //$linkToNext = ($nextPage > $totalPage ? '' : link_to(PAGE_NEXT, $urlToNext));
	        $linkToPage = '';
	        for ($i = $minPage; $i <= $maxPage; $i++)
	        {
	            $urlToPage = sprintf($url, $i);
	            $linkToPage.=($i == $page ?'<li><a class="page_current">'.$i.'</a></li>' :'<li><a href="'.$urlToPage.'">'.$i.'</a></li>'.'');
	        }
	        echo  '<div>Trang: '.
	        '
	                     <ul>   
	                         <li>'.$linkToFirst.'</li>'.                         
	        $linkToPage.
	        '<li>'.$linkToLast.'</li>
	                     </ul>
	                   </div>
	               ';        
	    }
	}
	
	static public function showPageAjax($totalPage,$task, $page, $updateZone, $userId, $type=0, $time=1)
	{
	    if ($totalPage > 1)
	    {
	        $minPage = 0;
	        $maxPage = 0;
	        $previousPage = 0;
	        $nextPage = 0;
	        mypaging::paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);
	
	        /*$urlToFirst = sprintf($url, 1);
	        $urlToPrevious = sprintf($url, $previousPage);
	        $urlToNext = sprintf($url, $nextPage);
	        $urlToLast = sprintf($url, $totalPage);*/
	        //$linkToFirst = ($minPage == 1 ? '' : link_to_remote(1, array('update' => $updateZone, 'url' => $urlToFirst)).'...');
	        if ($minPage == 1)
	        {
	            $linkToFirst = '';
	        }
	        else
	        if ($minPage == 2)
	        {
	            #$linkToFirst = link_to_remote(1, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToFirst, 'script'=>true));
	        	$linkToFirst = " <a href=\"javascript:showresult('$updateZone','$task','$userId','$type','$time','1')\">1</a>";
	        }
	        else
	        {
	            #$linkToFirst = link_to_remote(1, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToFirst,'script'=>true)).'...';
	            $linkToFirst = " <a href=\"javascript:showresult('$updateZone','$task','$userId','$type','$time','1')\">1</a>...";
	        }
	
	        if ($maxPage == $totalPage)
	        {
	            $linkToLast = '';
	        }
	        else
	        if ($maxPage == $totalPage - 1)
	        {
	            #$linkToLast = link_to_remote($totalPage, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToLast, 'script'=>true));
	            $linkToLast = " <a href=\"javascript:showresult('$updateZone','$task','$userId','$type','$time','$totalPage')\">$totalPage</a>";
	        }
	        else
	        {
	            #$linkToLast = '...'.link_to_remote($totalPage, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToLast, 'script'=>true));
	            $linkToLast = " ...<a href=\"javascript:showresult('$updateZone','$task','$userId','$type','$time','$totalPage')\">$totalPage</a>";
	        }
	
	        //$linkToNext = ($nextPage > $totalPage ? '' : link_to_remote(PAGE_NEXT, array('update' => $updateZone, 'url' => $urlToNext)));
	        $linkToPage = '';
	        for ($i = $minPage; $i <= $maxPage; $i++)
	        {
	            #$urlToPage = sprintf($url, $i);
	            #$linkToPage.=($i == $page ?'<li><a class="page_current">'.$i.'</a></li>' :'<li>'.link_to_remote($i, array('update' => array('success'=>$updateZone,'failure'=>'error'), 'url' => $urlToPage, 'script'=>true)).'</li>'.'');
	            $linkToPage.= ($i == $page ? '<li><a class="page_current">'.$i.'</a></li>':"<a href=\"javascript:showresult('$updateZone','$task','$userId','$type','$time','$i')\">$i</a>");
	        }
	        echo  '<div class="paging_comment row clearfix">'.
	        	'<div class="paging">Trang: 
	                     <ul>                                    
	                         <li>'.$linkToFirst.'</li>'.                         
	        					$linkToPage.
	                        '<li>'.$linkToLast.'</li>
	                     </ul>
	                   </div>
	               </div>';        
	    }
	}
}
?>